import java.util.ArrayList;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-09-08
 * Time: 16:42
 */

//顺序表
class MyArrayList {
    private int[] data;
    private int capacity = 10;//容量
    private int size = 0;//数组元素的个数

    public MyArrayList() {
        data = new int[capacity];
    }

    //添加
    public void add(int val) {
        //满了,扩容
        if (size >= capacity) {
            data = Arrays.copyOf(data, capacity * 2);
        }
        data[size] = val;
        size++;
    }

    //删除
    public void remove(int index) {
        if (size == 0) {
            //数组为空
            return;
        }
        if (index >= size) {
            return;
        }
        //挪数据
        int cur = index;
        while (true) {
            data[cur] = data[cur + 1];
            cur++;
            if (cur == size) {
                break;
            }
        }
        size--;
    }

    //查找
    public int get(int index) {
        if (index >= size || index < 0) {
            return -1;
        }

        int ret = data[index];

        return ret;
    }

    @Override
    public String toString() {
        if (size == 0) {
            return null;
        }
        StringBuilder str = new StringBuilder();
        str.append("[");
        for (int i = 0; i < size; i++) {
            str.append(data[i]);
            if (i < size - 1) {
                str.append(", ");
            }
        }
        str.append("]");
        return str.toString();
    }
}

public class demo6 {
    public static void main(String[] args) {
        MyArrayList arrayList = new MyArrayList();
        arrayList.add(1);
        arrayList.add(2);
        arrayList.add(3);
        arrayList.add(4);
        arrayList.add(4);
        arrayList.add(4);
        arrayList.add(4);
        arrayList.add(4);
        arrayList.add(4);
        arrayList.add(4);
        arrayList.add(4);
        arrayList.add(4);
        arrayList.add(4);
        arrayList.add(4);
        arrayList.add(4);
        arrayList.add(4);
        arrayList.add(4);
        arrayList.add(4);
        arrayList.add(4);
        arrayList.add(4);
        System.out.println(arrayList);
    }
    public static void main1(String[] args) {
        MyArrayList arrayList = new MyArrayList();
        arrayList.add(1);
        arrayList.add(2);
        arrayList.add(3);
        arrayList.add(4);
        System.out.println(arrayList);
        arrayList.remove(0);
        System.out.println(arrayList);

        arrayList.remove(0);
        System.out.println(arrayList);
        arrayList.remove(0);
        System.out.println(arrayList);
        arrayList.remove(0);
        System.out.println(arrayList);

    }
}
